<?php

/*****************************************************************************
 * 
 *  Copyright 2009 Sylvain Dudoit (contact : sylvain.dudoit at gmail.com)
 *  
 *  This file is part of Poupitchi.
 *
 *  Poupitchi is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  Poupitchi is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with Poupitchi.  If not, see <http://www.gnu.org/licenses/>.
 *
 *****************************************************************************/


///////////////////////////////////////////////////////////////////////////////
// Include db

require_once("db.".P_DB.".php");

///////////////////////////////////////////////////////////////////////////////
// Check form submition

if (isset($_POST['login'])) {
	if ($_POST['action'] == "update") {
		unset($_POST['action']);
  	setUser($_POST);
	} else if ($_POST['action'] == "add") {
		addUser($_POST['login'], $_POST['password'], $_POST['email'], $_POST['fname'], $_POST['lname']);
	}
}
if (isset($_POST['action'])) {
	if ($_POST['action'] == "password") {
		changePassword($P_USER, $_POST['newpassword'], $_POST['confirmpassword']);
	}
}
if (isset($_GET['delete'])) {
  removeUser($_GET['delete']);
}

///////////////////////////////////////////////////////////////////////////////
// Define list

$start   = isset($_GET['start']) ? $_GET['start'] : 0;
$total   = countUsers();
$nbitems = P_NB_USERS;

$users   = getUsers($nbitems, $_GET['start']);
$logged  = getUser($P_USER);

///////////////////////////////////////////////////////////////////////////////
// Print page

?>

<?php eLookBloc(); ?>

<div id="bloc_users">
	<div id="link_form">
		<?php if (!$P_ADMIN) : ?>
		<a href="#bloc_form">&gt; <?php etr("Edit profile") ?> &lt;</a>
		<?php else : ?>
		<a href="#bloc_admin_form">&gt; <?php etr("Add user") ?> &lt;</a>
		<?php endif; ?>
	</div>
	<?php foreach($users as $user) : ?>
	<div class="bloc_user">	
		<h2> <?php e($user['fname']." ".$user['lname'])?> </h2>
		<?php eFace($user['login']) ?>
		<table>
       <tr>
        <td><?php e($user['email'])?></td>
        <th><?php etr("Email")?></th>
        </tr>    
       <tr>
        <td><?php e(nl2br($user['addresses']))?></td>
           <th><?php etr("Addresses")?></th>
        </tr>    
       <tr>
        <td><?php e(nl2br($user['phones']))?></td>
           <th><?php etr("Phones")?></th>
        </tr>    
       <tr>
        <td><?php e(nl2br($user['others_emails']))?></td>
           <th><?php etr("Others emails")?></th>
        </tr>    
       <tr>
        <td><?php e(nl2br($user['im_accounts']))?></td>
           <th><?php etr("MSN/AIM ...")?></th>
        </tr>    
       <tr>
        <td><?php e(nl2br($user['websites']))?></td>
           <th><?php etr("Web sites")?></th>
        </tr>    
	    </table>
		<h4> 
				<?php etr("Modified at")?> : <?php e(date(P_DATE, $user['date']))?> 
			  <?php if ($P_ADMIN) : ?>
	    		<a href="index.php?action=show&module=users&delete=<?php e($user['login']); ?>"
	    			 onclick="return confirm('<?php etr("Are you sure you want to delete ?"); ?>')">
	    				[ <?php etr("Delete"); ?> ] 
	    		</a>
    		<?php endif; ?>
		</h4>
	</div>	
	<?php endforeach;?>
	<?php ePagination($start, $total, $nbitems); ?>	
	<?php if (!$P_ADMIN) : ?>
	<div id="bloc_form">
		<h2>&gt; <?php etr("Edit profile") ?> &lt;</h2>
	  <form action="index.php?action=show&module=users" method="post">
	    <input type="hidden" name="login" value="<?php e($P_USER); ?>"/>
	    <input type="hidden" name="action" value="update"/>
	    <table>
	      <tr>
          <th><?php etr("First name")?></th>
        	<td><input type="text" class="title" name="fname" size="30" 
        			   value="<?php e($logged['fname']); ?>" /></td>
        </tr>
       	<tr>
          <th><?php etr("Last name")?></th>
        	<td><input type="text" class="title" name="lname" size="30" 
        			   value="<?php e($logged['lname']); ?>" /></td>
        </tr>
       	<tr>
          <th><?php etr("Email")?></th>
        	<td><input type="text" class="title" name="email" size="30" 
        			   value="<?php e($logged['email']); ?>" /></td>
        </tr>
       	<tr>
          <th><?php etr("Addresses")?></th>
        	<td><textarea class="body" name="addresses" cols="25" 
        				  rows="6"><?php e($logged['addresses']); ?></textarea></td>
        </tr>
       	<tr>
          <th><?php etr("Phones")?></th>
        	<td><textarea class="body" name="phones" cols="25" 
        				  rows="6"><?php e($logged['phones']); ?></textarea></td>

        </tr>
       	<tr>
          <th><?php etr("Others emails")?></th>
        	<td><textarea class="body" name="others_emails" cols="25" 
        				  rows="6"><?php e($logged['others_emails']); ?></textarea></td>
        </tr>
       	<tr>
          <th><?php etr("MSN/AIM ...")?></th>
        	<td><textarea class="body" name="im_accounts" cols="25" 
        				  rows="6"><?php e($logged['im_accounts']); ?></textarea></td>
        </tr>	        
       	<tr>
          <th><?php etr("Web sites")?></th>
        	<td><textarea class="body" name="websites" cols="25" 
        				  rows="6"><?php e($logged['websites']); ?></textarea></td>
        </tr>	        
        <tr>
          <th>&nbsp;</th>
        	<td><input type="submit" class="submit" value="<?php etr("Save")?>"/></td>
        </tr>		        
	    </table>
	  </form>
	</div>
	<?php else : ?>
  <div id="bloc_admin_form">
  	<h2>&gt; <?php etr("Add user") ?> &lt;</h2>
		<form action="index.php?action=show&module=users" method="post">
	    <input type="hidden" name="action" value="add"/>
			<table>
            <tr>
                <th><?php etr("Login"); ?></th>
                <td><input type="text" class="title" name="login" size="15" value="" /></td>
            </tr>
            <tr>
                <th><?php etr("Password"); ?></th>
                <td><input type="password" class="title" name="password" size="15" value="" /></td>
          </tr>
          <tr>
              <th><?php etr("Email"); ?></th>
              <td><input type="text" class="title" name="email" size="30" value="" /></td>
          </tr>
	       	<tr>
	           	<th><?php etr("First name"); ?></th>
	        	<td><input type="text" class="title" name="fname" size="30" value="" /></td>
	        </tr>
	       	<tr>
	           	<th><?php etr("Last name"); ?></th>
	        	<td><input type="text" class="title" name="lname" size="30" value="" /></td>
	        </tr>
	        <tr>
	            <th>&nbsp;</th>
	        	<td><input type="submit" class="submit" value="<?php etr("Save"); ?>"/></td>
	        </tr>		        
	    </table>
	</form>
  </div>
	<?php endif; ?>  
	<div id="bloc_password_form">
  	<h2>&gt; <?php etr("Change password") ?> &lt;</h2>
		<form action="index.php?action=show&module=users" method="post">
	    <input type="hidden" name="action" value="password"/>
			<table>
          <tr>
              <th><?php etr("New password"); ?></th>
              <td><input type="password" class="title" name="newpassword" size="15" value="" /></td>
          </tr>
          <tr>
              <th><?php etr("Confirm password"); ?></th>
              <td><input type="password" class="title" name="confirmpassword" size="15" value="" /></td>
          </tr>
	        <tr>
	            <th>&nbsp;</th>
	        	<td><input type="submit" class="submit" value="<?php etr("Save"); ?>"
	        						 onclick="return confirm('<?php etr("Are you sure to change password ?"); ?>')" />
	          </td>
	        </tr>		        
	    </table>
	</form>
  </div>
</div>
